package com.windchime.blog.result;

import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回结果类
 */

@Data
public class R implements Serializable {

    private Integer code;

    private String msg;

    private Long count;

    private Map<String, Object> data = new HashMap<>();

    /**
     * 把构造方法私有化
     */
    private R() {}

    /**
     * 成功静态方法
     * @return
     */
    public static R ok() {
        R r = new R();
        r.setCode(ResultCode.SUCCESS);
        r.setMsg("成功");
        r.setCount(1L);
        return r;
    }

    /**
     * 失败静态方法
     * @return
     */
    public static R error() {
        R r = new R();
        r.setCode(ResultCode.ERROR);
        r.setMsg("失败");
        r.setCount(0L);
        return r;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R msg(String message) {
        this.setMsg(message);
        return this;
    }

    public R count(Long count) {
        this.setCount(count);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

}
